/*
package com.yunc.modules.shop.queue;

import com.yunc.modules.shop.entity.ShopRandomCode;
import com.yunc.modules.shop.service.IShopItemService;
import com.yunc.modules.shop.service.IShopItemSpecsService;
import com.yunc.modules.shop.service.IShopRandomCodeService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

*/
/**
 * 消费者
 *
 * @author taocongcong
 * @create 2019-10-25 7:47
 *//*

@Component
@Slf4j
public class ShopRabbitMqConsumer {

    @Autowired
    private IShopItemSpecsService shopItemSpecsService;
    @Autowired
    private IShopItemService shopItemService;
    @Autowired
    private IShopRandomCodeService shopRandomCodeService;
    @Autowired
    private RedissonClient redissonClient;

    @RabbitListener(queues = {"ITEM_QUEUE"})
    public void receiver(String msg) {
        // 队列是正确的，当不是集群只有一个程序在跑时，依次进入队列
        // 但是由于集群进入，集群内分开进入，所以修改数据库时，出现了并发问题
        // 1、使用分布式锁解决
        // 2、使用数据库锁解决
        String[] split = msg.split(",");
        String userId = split[0];
        String specsSeq = split[1];
        String orderNumber = split[2];
        // 使用定时任务同步库存
        // 如果采用分布式锁呢
        ShopRandomCode shopRandomCode = new ShopRandomCode();
        shopRandomCode.setUserId(userId);
        shopRandomCode.setCode(orderNumber);
        shopRandomCodeService.save(shopRandomCode);
        log.info("将队列中信息取出，保存至数据库，保存主键为{}", shopRandomCode.getId());
        // 如果采用分布式锁呢
        */
/*RLock rlock = redissonClient.getLock(REDISSON_LOCK_ITEMSPECSSEQ);
        // 设置锁超时时间，防止异常造成死锁
        rlock.lock(10, TimeUnit.SECONDS);
        try {
            ShopRandomCode shopRandomCode = new ShopRandomCode();
            shopRandomCode.setCode(orderNumber);
            shopRandomCodeService.save(shopRandomCode);
            // 执行业务逻辑
            ShopItemSpecs shopItemSpecs = shopItemSpecsService.getById(Long.valueOf(specsSeq));
            shopItemSpecs.setStock(shopItemSpecs.getStock() - 1);
            log.info("将队列中信息取出，保存至数据库，数据库库存为：{}", shopItemSpecs.getStock());
            shopItemSpecsService.updateById(shopItemSpecs);
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            rlock.unlock();
        }*//*

    }
}
*/
